package com.easefun.polyv.livecloudclass.modules.media.danmu;

import android.content.Context;
import android.text.Editable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.easefun.polyv.R;
import com.easefun.polyv.livecommon.ui.widget.PLVOrientationSensibleLinearLayout;
import com.lxj.xpopup.core.BottomPopupView;
import com.plv.socket.event.chat.PLVChatQuoteVO;
import com.plv.thirdpart.blankj.utilcode.util.BarUtils;
import com.plv.thirdpart.blankj.utilcode.util.KeyboardUtils;
import com.plv.thirdpart.blankj.utilcode.util.ScreenUtils;
import com.plv.thirdpart.blankj.utilcode.util.ToastUtils;

import org.jetbrains.annotations.NotNull;

public class DialogInputView extends BottomPopupView {
    private Context context;
    private ConstraintLayout chatSendMsgQuoteLayout;
    private TextView chatSendMsgQuoteTv;
    private ImageView chatSendMsgQuoteCloseIv;
    private PLVOrientationSensibleLinearLayout llSendMessage;
    private EditText etSendMessage;
    private TextView tvSendMessage;

    public DialogInputView(@NonNull @NotNull Context context) {
        super(context);
        this.context = context;
    }

    public LinearLayout llInput;

    @Override
    protected int getImplLayoutId() {
        return R.layout.plvlc_player_message_send_layout;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        RelativeLayout rlLayout = findViewById(R.id.rlLayout);
        rlLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        llInput = findViewById(R.id.llInput);
        chatSendMsgQuoteLayout = findViewById(R.id.plvlc_chat_send_msg_quote_layout);
        chatSendMsgQuoteTv = findViewById(R.id.plvlc_chat_send_msg_quote_tv);
        chatSendMsgQuoteCloseIv = findViewById(R.id.plvlc_chat_send_msg_quote_close_iv);
        llSendMessage = findViewById(R.id.ll_send_message);
        etSendMessage = findViewById(R.id.et_send_message);
        tvSendMessage = findViewById(R.id.tv_send_message);
        updateChatQuote();
        llSendMessage.post(new Runnable() {
            @Override
            public void run() {
                KeyboardUtils.showSoftInput(etSendMessage);
            }
        });
        setListener();
    }

    private void setListener() {

        chatSendMsgQuoteCloseIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendMessageListener != null) {
                    sendMessageListener.onCloseChatQuote();
                    updateChatQuote();
                }
            }
        });
        tvSendMessage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMessage();
                dismiss();
            }
        });
        etSendMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {/**/}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {/**/}

            @Override
            public void afterTextChanged(Editable s) {
                boolean enable = TextUtils.isEmpty(etSendMessage.getText());
                tvSendMessage.setEnabled(!enable);
            }
        });
        etSendMessage.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND) {
                    sendMessage();
                    return true;
                }
                return false;
            }
        });
        llSendMessage.setOnPortrait(new Runnable() {
            @Override
            public void run() {
                dismiss();
            }
        });
    }
    private void sendMessage() {
        String msg = etSendMessage.getText().toString();
        if (msg.trim().length() == 0) {
            ToastUtils.showLong(R.string.plv_chat_toast_send_text_empty);
            return;
        }
        etSendMessage.setText("");
        //发送消息
        if (sendMessageListener != null) {
            sendMessageListener.onSend(msg, sendMessageListener.getChatQuoteContent());
        }
        KeyboardUtils.hideSoftInput(etSendMessage);
       dismiss();
    }
    private void updateChatQuote() {
        final PLVChatQuoteVO chatQuoteVO = sendMessageListener == null ? null : sendMessageListener.getChatQuoteContent();
        if (chatQuoteVO == null) {
            chatSendMsgQuoteLayout.setVisibility(View.GONE);
            return;
        }
        chatSendMsgQuoteLayout.setVisibility(View.VISIBLE);

        CharSequence quoteMsg = chatQuoteVO.getObjects() == null || chatQuoteVO.getObjects().length == 0 ? "" : (CharSequence) chatQuoteVO.getObjects()[0];
        final boolean isImageContent = chatQuoteVO.getContent() == null && chatQuoteVO.getImage() != null && chatQuoteVO.getImage().getUrl() != null;
        if (isImageContent) {
            quoteMsg = "[图片]";
        }
        chatSendMsgQuoteTv.setText(new SpannableStringBuilder(chatQuoteVO.getNick()).append("：").append(quoteMsg));
    }

    @Override
    protected int getMaxWidth() {
        return ScreenUtils.getScreenWidth() + BarUtils.getStatusBarHeight();
    }


    private IPLVLCLandscapeMessageSender.OnSendMessageListener sendMessageListener;

    public void setListener(IPLVLCLandscapeMessageSender.OnSendMessageListener listener) {
        this.sendMessageListener = listener;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (etSendMessage!=null){
            KeyboardUtils.hideSoftInput(etSendMessage);
        }
    }
}
